package com.lsf.dunlian.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.lsf.dunlian.R;
import com.lsf.dunlian.config.PreferenceUtil;
import com.lsf.dunlian.config.Urls;
import com.lsf.dunlian.constant.Constant;
import com.lsf.dunlian.databinding.ActivityAttendanceRecordBinding;
import com.lsf.dunlian.databinding.ActivityCreateNoticeBinding;
import com.lsf.dunlian.entity.AllNoticeRecordEntity;
import com.lsf.dunlian.entity.BaseEntity;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CreateNoticeActivity extends Activity {
    private ActivityCreateNoticeBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseUtils.setStatusBar(this);
        binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_create_notice, null, false);
        setContentView(binding.getRoot());
        binding.fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                create();
            }
        });
    }
    private void create(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(Urls.NOTICE_CREATE+"?userId="+ PreferenceUtil.getSharedPreference(Constant.USERID)+"&content="+binding.content.getText().toString()).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) { }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    BaseEntity vo = new Gson().fromJson(result, BaseEntity.class);
                    runOnUiThread(() -> {
                        if (vo.code!=200){
                            Toast.makeText(CreateNoticeActivity.this,vo.msg,Toast.LENGTH_SHORT).show();
                        }else {
                            finish();
                        }
                    });
                }
            }
        });
    }
}
